package com.bcsq.todolistdemo.widget;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.widget.DatePicker;
import android.widget.TimePicker;

import java.util.Calendar;

public class DateAndTimePictureDialog {
    private static int year;
    private static int month;
    private static int day;
    private static int hour;
    private static int minute;
    private static OnDateAndTimePickedListener onDateAndTimePickedListener;

    public static void showDateAndTimePicterDialog(Context context, OnDateAndTimePickedListener listener){
        showDatePicterDialog( context);
        onDateAndTimePickedListener = listener;
    }

    /**
     * 显示日期
     */
    private static void showDatePicterDialog(final Context context){
        Calendar calendar = Calendar.getInstance() ;
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
//                Toast.makeText(mContext, "日期" + i + i1 + i2, Toast.LENGTH_SHORT).show();
                showTimePictureDialog(context);
                year = i;
                month = i1 + 1;
                day = i2;
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

        datePickerDialog.show();


    }

    /**
     * 显示时间
     */
    private static void showTimePictureDialog(Context context){
        TimePickerDialog timePickerDialog = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int i, int i1) {
                hour = i;
                minute = i1;
//                Toast.makeText(mContext, "时间" + i + i1, Toast.LENGTH_SHORT);//length_long
                String time = year + "/" + (month < 10 ? "0" + month : month) + "/" + (day < 10 ? "0" + day : day)
                        + " " + (hour < 10 ?  "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute) ;

                if (onDateAndTimePickedListener != null){
                    onDateAndTimePickedListener.onDateAndTimePicked(time);
                }
            }
        }, 0, 0, true);

        timePickerDialog.show();
    }

    public interface OnDateAndTimePickedListener{
        void onDateAndTimePicked(String dateTime);
    }
}
